package newcoder;

/**
 * @Author 肖地洋
 * @Date 2022/7/12 15:44
 */
public class NC201 {

    public static void main(String[] args) {
        int[][] nums = {{1,2,3},{4,5,6},{7,8,9}};
        int col = nums.length,row =nums.length;
        int[] res = new int[col*row];
        int id =0,i = 0, j = 0;
        //在四条不同的边拐弯的方向不一样
        res[id++] =nums[i][j];
        while (id<col*row){
            int[] t = turn(i,j,col,row);
            i = t[0];
            j = t[1];
            if(t[2]==1){
                while (i>=0&&j<row){
                    res[id++] = nums[i--][j++];
                }
                i++;
                j--;
            }else if(t[2]==-1){
                while (i<col&&j>=0){
                    res[id++] = nums[i++][j--];
                }
                i--;
                j++;
            }else{
                res[id++] = nums[i][j];
            }
        }
        System.out.println(res);
    }
    public static int[] turn(int i,int j,int c,int r){

        if(i==0&&j==0) return new int[]{i,j+1,-1};
        if(i==0&&j==r-1) return new int[]{i+1,j,-1};
        if(i==c-1&&j==0) return new int[]{i,j+1,1};
        if(i==c-1&&j==r-1) return new int[]{i,j+1,0}; //结束
        if(i==0) return new int[]{i,j+1,-1};
        if(i==c-1) return new int[]{i,j+1,1};
        if(j==0) return new int[]{i+1,j,1};
        if(j==r-1) return new int[]{i+1,j,-1};

        return null;
    }

}
